package cn.shenlong.portal.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.shenlong.common.Constants;
import cn.shenlong.common.Pagination;
import cn.shenlong.portal.domain.Link;
import cn.shenlong.portal.service.LinkService;
import cn.shenlong.util.UploadUtil;

@Controller
@RequestMapping("/link/admin")
public class LinkAController {
	@Autowired
	private LinkService linkService;

	@RequestMapping("/index")
	public String index() {
		return "/server/link/index";
	}

	@RequestMapping("/list")
	public String main(Link link, Integer pageSize, Integer pageNo,
			ModelMap model) {
		if (pageSize == null) {
			pageSize = 20;
		}
		if (pageNo == null) {
			pageNo = 1;
		}

		Pagination p = linkService.findLink(link, null, pageSize, pageNo);
		model.addAttribute("link", link);
		model.addAttribute("pagination", p);
		return "/server/link/list";
	}

	@RequestMapping("/addLink")
	public String addLink() {
		return "/server/link/add";
	}

	@RequestMapping("/showLink")
	public String showLink(Long linkId, ModelMap model) {
		Link link = this.linkService.findLinkByLinkId(linkId);
		model.put("link", link);
		return "/server/link/show";
	}

	@RequestMapping("/saveLink")
	public String saveLink(Link link, ModelMap model,
			MultipartHttpServletRequest request) {
		if (link == null || link.getLinkId() == null) { // 新增
			try {
				List<Map<String, Object>> resultList = UploadUtil.uploadImage(
						request, Constants.LINK_IMG_URI);
				if (!resultList.isEmpty()) {
					link.setImageUrl(resultList.get(0).get("imagePath")
							.toString());
				}
				linkService.save(link);
				if (link != null)
					model.put("msg", "success");
			} catch (Exception e) {
				model.put("msg", "failed");
			}
			return addLink();
		} else {// 编辑
			Link origionLink = linkService.findLinkByLinkId(link.getLinkId());
			origionLink.setLinkName(link.getLinkName());
			origionLink.setLinkType(link.getLinkType());
			origionLink.setLinkUrl(link.getLinkUrl());
			try {
				List<Map<String, Object>> resultList = UploadUtil.uploadImage(
						request, Constants.LINK_IMG_URI);
				if (!resultList.isEmpty()) {// 有上传图片
					// 删除原来的图片
					UploadUtil.deleteForUrl(origionLink.getImageUrl());

					// 设置新的路径
					origionLink.setImageUrl(resultList.get(0).get("imagePath")
							.toString());
				}
				link = linkService.updateLink(origionLink);
				if (link != null)
					model.put("msg", "success");
			} catch (Exception e) {
				model.put("msg", "failed");
				model.put("link", link);
			}
			return this.editLink(link.getLinkId(), model);
		}

	}

	@RequestMapping("/editLink")
	public String editLink(Long linkId, ModelMap model) {
		if (model == null || model.get("link") == null) {
			Link link = this.linkService.findLinkByLinkId(linkId);
			model.put("link", link);
		}
		return "/server/link/edit";
	}

	@RequestMapping("/deleteLink")
	@ResponseBody
	public Map<String, Object> deleteLink(Long linkId, String linkIdStr,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (linkId == null) {
				String[] tmp = linkIdStr.split(",");
				Long[] linkIds = new Long[tmp.length];
				for (int i = 0; i < tmp.length; i++) {
					linkIds[i] = Long.parseLong(tmp[i]);
				}
				linkService.deleteLink(linkIds);
			} else {
				linkService.deleteLink(new Long[] { linkId });
			}
			resultMap.put("result", "删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "删除失败！原因：" + e.toString());
		}
		return resultMap;
	}
}
